package com.gitee.wsl.android.ui.sample.icon


import android.animation.ValueAnimator
import android.graphics.*
import android.view.animation.AnticipateInterpolator

/**
 * Created by codeest on 16/11/7.
 *
 * 论如何从直线到曲线再到三角形 =。=
 */
class ENPlayView : AnimatedIcon(){
    var currentState = STATE_PAUSE
        private set
    private var mPaint: Paint? = null
    private var mBgPaint: Paint? = null
    private var mWidth = 0
    private var mHeight = 0
    private var mCenterX = 0
    private var mCenterY = 0
    private var mCircleRadius = 0
    private var mRectF: RectF? = null
    private var mBgRectF: RectF? = null
    private var mFraction = 1f
    private var mPath: Path? = null
    private var mDstPath: Path? = null
    private var mPathMeasure: PathMeasure? = null
    private var mPathLength = 0f
    private var mDuration = 0

   /* constructor(context: Context?) : super(context) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.play)
        val lineColor = ta.getColor(R.styleable.play_play_line_color, DEFAULT_LINE_COLOR)
        val bgLineColor = ta.getColor(R.styleable.play_play_bg_line_color, DEFAULT_BG_LINE_COLOR)
        val lineWidth = ta.getInteger(R.styleable.play_play_line_width, DEFAULT_LINE_WIDTH)
        val bgLineWidth = ta.getInteger(R.styleable.play_play_bg_line_width, DEFAULT_BG_LINE_WIDTH)
        ta.recycle()
        setLayerType(LAYER_TYPE_SOFTWARE, null)

    }*/

    init {
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.strokeCap = Paint.Cap.ROUND
        mPaint!!.color = DEFAULT_LINE_COLOR
        mPaint!!.strokeWidth = DEFAULT_LINE_WIDTH.toFloat()
        mPaint!!.pathEffect = CornerPathEffect(1.0f)
        mBgPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mBgPaint!!.style = Paint.Style.STROKE
        mBgPaint!!.strokeCap = Paint.Cap.ROUND
        mBgPaint!!.color = DEFAULT_BG_LINE_COLOR
        mBgPaint!!.strokeWidth = DEFAULT_BG_LINE_WIDTH.toFloat()
        mPath = Path()
        mDstPath = Path()
        mPathMeasure = PathMeasure()
        mDuration = DEFAULT_DURATION
    }

     fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        mWidth = w * 9 / 10
        mHeight = h * 9 / 10
        mCircleRadius = mWidth / 10
        mCenterX = w / 2
        mCenterY = h / 2
        mRectF = RectF(
            (mCenterX - mCircleRadius).toFloat(), mCenterY + 0.6f * mCircleRadius,
            (mCenterX + mCircleRadius).toFloat(), mCenterY + 2.6f * mCircleRadius
        )
        mBgRectF = RectF(
            (mCenterX - mWidth / 2).toFloat(),
            (mCenterY - mHeight / 2).toFloat(),
            (mCenterX + mWidth / 2).toFloat(),
            (mCenterY + mHeight / 2).toFloat()
        )
        mPath!!.moveTo((mCenterX - mCircleRadius).toFloat(), mCenterY + 1.8f * mCircleRadius)
        mPath!!.lineTo((mCenterX - mCircleRadius).toFloat(), mCenterY - 1.8f * mCircleRadius)
        mPath!!.lineTo((mCenterX + mCircleRadius).toFloat(), mCenterY.toFloat())
        mPath!!.close()
        mPathMeasure!!.setPath(mPath, false)
        mPathLength = mPathMeasure!!.length
    }

    override fun draw(canvas: Canvas) {

        canvas.drawCircle(
            mCenterX.toFloat(),
            mCenterY.toFloat(),
            (mWidth / 2).toFloat(),
            mBgPaint!!
        )
        if (mFraction < 0) {    //嗷~~ 弹性部分
            canvas.drawLine(
                (mCenterX + mCircleRadius).toFloat(),
                mCenterY - 1.6f * mCircleRadius + 10 * mCircleRadius * mFraction,
                (
                        mCenterX + mCircleRadius).toFloat(),
                mCenterY + 1.6f * mCircleRadius + 10 * mCircleRadius * mFraction,
                mPaint!!
            )
            canvas.drawLine(
                (mCenterX - mCircleRadius).toFloat(),
                mCenterY - 1.6f * mCircleRadius,
                (
                        mCenterX - mCircleRadius).toFloat(),
                mCenterY + 1.6f * mCircleRadius,
                mPaint!!
            )
            canvas.drawArc(mBgRectF!!, -105f, 360f, false, mPaint!!)
        } else if (mFraction <= 0.3) {  //嗷~~ 右侧直线和下方曲线
            canvas.drawLine(
                (mCenterX + mCircleRadius).toFloat(),
                mCenterY - 1.6f * mCircleRadius + mCircleRadius * 3.2f / 0.3f * mFraction,
                (
                        mCenterX + mCircleRadius).toFloat(),
                mCenterY + 1.6f * mCircleRadius,
                mPaint!!
            )
            canvas.drawLine(
                (mCenterX - mCircleRadius).toFloat(),
                mCenterY - 1.6f * mCircleRadius,
                (
                        mCenterX - mCircleRadius).toFloat(),
                mCenterY + 1.6f * mCircleRadius,
                mPaint!!
            )
            if (mFraction != 0f) canvas.drawArc(
                mRectF!!,
                0f,
                180f / 0.3f * mFraction,
                false,
                mPaint!!
            )
            canvas.drawArc(
                mBgRectF!!,
                -105 + 360 * mFraction,
                360 * (1 - mFraction),
                false,
                mPaint!!
            )
        } else if (mFraction <= 0.6) {  //嗷~~ 下方曲线和三角形
            canvas.drawArc(
                mRectF!!,
                180f / 0.3f * (mFraction - 0.3f),
                180 - 180f / 0.3f * (mFraction - 0.3f),
                false,
                mPaint!!
            )
            mDstPath!!.reset()
            mPathMeasure!!.getSegment(
                0.02f * mPathLength,
                0.38f * mPathLength + 0.42f * mPathLength / 0.3f * (mFraction - 0.3f),
                mDstPath,
                true
            )
            canvas.drawPath(mDstPath!!, mPaint!!)
            canvas.drawArc(
                mBgRectF!!,
                -105 + 360 * mFraction,
                360 * (1 - mFraction),
                false,
                mPaint!!
            )
        } else if (mFraction <= 0.8) {  //嗷~~ 三角形
            mDstPath!!.reset()
            mPathMeasure!!.getSegment(
                0.02f * mPathLength + 0.2f * mPathLength / 0.2f * (mFraction - 0.6f),
                0.8f * mPathLength + 0.2f * mPathLength / 0.2f * (mFraction - 0.6f),
                mDstPath,
                true
            )
            canvas.drawPath(mDstPath!!, mPaint!!)
            canvas.drawArc(
                mBgRectF!!,
                -105 + 360 * mFraction,
                360 * (1 - mFraction),
                false,
                mPaint!!
            )
        } else {    //嗷~~ 弹性部分
            mDstPath!!.reset()
            mPathMeasure!!.getSegment(
                10 * mCircleRadius * (mFraction - 1), mPathLength,
                mDstPath, true
            )
            canvas.drawPath(mDstPath!!, mPaint!!)
        }
    }

    override fun startAnimation() {
        if (currentState == STATE_PLAY) {
            return
        }
        currentState = STATE_PLAY
        val valueAnimator = ValueAnimator.ofFloat(1f, 100f)
        valueAnimator.duration = mDuration.toLong()
        valueAnimator.interpolator = AnticipateInterpolator()
        valueAnimator.addUpdateListener { valueAnimator ->
            mFraction = 1 - valueAnimator.animatedFraction
            invalidateSelf()
        }
        if (!valueAnimator.isRunning) {
            valueAnimator.start()
        }
    }


     fun pause() {
        if (currentState == STATE_PAUSE) {
            return
        }
        currentState = STATE_PAUSE
        val valueAnimator = ValueAnimator.ofFloat(1f, 100f)
        valueAnimator.duration = mDuration.toLong()
        valueAnimator.interpolator = AnticipateInterpolator()
        valueAnimator.addUpdateListener { valueAnimator ->
            mFraction = valueAnimator.animatedFraction
            invalidateSelf()
        }
        if (!valueAnimator.isRunning) {
            valueAnimator.start()
        }
    }

     fun setDuration(duration: Int) {
        mDuration = duration
    }

    companion object {
        var STATE_PLAY = 0
        var STATE_PAUSE = 1
        var DEFAULT_LINE_COLOR = Color.WHITE
        var DEFAULT_BG_LINE_COLOR = -0xbbb5b1
        var DEFAULT_LINE_WIDTH = 14
        var DEFAULT_BG_LINE_WIDTH = 12
        var DEFAULT_DURATION = 1200
    }
}